<?php
//供应商
namespace Common\Model;

class AdminModel extends BaseModel {

	//追加属性
    public $append = [
        'sex_text',
        'status_text',
        'atype_text',
        'stype_text',
        'avatar',
	];

    //头像
    function getAvatarAttr($val, $data){
        $avatar = $data['avatar'] ? : 'https://wages-zymaimai.oss-cn-beijing.aliyuncs.com/2023/03/02/6400606a88a09.jpg';
        return $avatar;
    }

    //性别
    function getSexTextAttr($val, $data){
        $text = '';
        if($data['aSex']==0){
            $text = '女';
        }else if($data['aSex']==1){
            $text = '男';
        }else if($data['aSex']==2){
            $text = '未知';
        }
        return $text;
    }

    //状态
    function getStatusTextAttr($val, $data){
        $text = '';
        if($data['aStatic']==0){
            $text = '禁用';
        }else if($data['aStatic']==1){
            $text = '正常';
        }
        return $text;
    }

    //类型
    function getAtypeTextAttr($val, $data){
        $array = array(
            0=>'管理员',
            1=>'业务员',
            2=>'财务',
            3=>'采购',
            4=>'仓库管理',
            5=>'厂长',
            6=>'小组管理',
            7=>'工厂',
            12=>'主任',
            10=>'未知',
        );
        $atype = $data['atype'];
        $text = $array[$atype];
        return $text;
    }

    //类型
    function getStypeTextAttr($val, $data){
        $text = '';
        //if($data['atype']==7){
            if($data['stype']==1){
                $text = '委外';
            }else{
                $text = '本厂';
            }
        //}
        return $text;
    }

    /**
     * 验证登录
     * @param int $form     //1就是默认，2:小程序 3:app
     * @return array
     */
    public function login($form=1, $_post){
        $aUser = $_post["aUser"];
        $aPwd = md5($_post["aPwd"]);
        $user = $this->where("aUser='{$aUser}' OR aTel='{$aUser}'")->find();
        if(!$user){
            $this->error = '账号不存在';
            return false;
        }
        if($aPwd!=$user['aPwd']){
            $this->error = '密码错误';
            return false;
        }
        if($user["aStatic"] == 0){
            //echo M()->_sql();exit;
            $this->error = '对不起，该账户已被系统管理员停用！';
            return false;
        }
        $types = $user['atype'];
        $aLoginNum = $rs["aLoginNum"] + 1;
        $data_cc["aLoginNum"] = $aLoginNum;
        $data_cc["aErrorPwdNum"] = 0;
        $data_cc['login_time'] = time();
        $this->where("aUser='{$aUser}' OR aTel='{$aUser}'")->save($data_cc);
        /*
		$wdata['aId'] = $user['aId'];
        $sdata['login_err'] = 0;
		$sdata['login_time'] = NOW_TIME;
		$sdata['login_ip'] = $_SERVER['REMOTE_ADDR'];
        $this->where($wdata)->save($sdata);
        */
		//重新赋值（部分信息不返回）
		//token
		$ut_model = D('Token');
        //Db或redis存储
        $r = $ut_model->setToken($user, $form, 1);
        if(!$r['status']){
            $this->error = 'Token生成失败';
            return false;
        }
        $user['token'] = $r['data'];
        return $user;
    }

    //格式化
    function listFormat($list){
        foreach($list as &$val){
            unset($val['aPwd']);
        }
        return $list;
    }

    //搜索后置
    function _after_search(){
        $map = array();
        if(I('aName')){
            //模糊搜索
            $map['aName'] = array('like','%'.I('aName').'%');
        }
        //dump($map);exit;
        return $map;
    }

   
}

?>